import requests
from jsonpath import jsonpath
import json


# 请求
def get_data(url):
    headers = {
        'Referer': 'https://news.163.com/',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36',
    }
    r = requests.get(url, headers=headers)
    if r.status_code == 200:
        return r.text

# 解析
def parse_data(data):
    text_data_2 = data.split('data_callback(')[1]
    text_data_3 = text_data_2.split(')')[0]
    # print(text_data_3)
    data_json = json.loads(text_data_3)  # json.loads()将str转为JSON数据的（dict、list）
    # print(type(data_json))
    t = jsonpath(data_json, '$..title')
    # print(t)
    for tit in t:
        print(tit)
        print('=' * 20)

if __name__ == '__main__':
    # 起始url  1页
    url = 'https://news.163.com/special/cm_yaowen20200213/?callback=data_callback'
    print('正在下载第1页')
    data = get_data(url)
    parse_data(data)
    # 还原2-5页url还原
    for i in range(2, 6):
        print('正在下载第{}页'.format(i))
        page_url = 'https://news.163.com/special/cm_yaowen20200213_0{}/?callback=data_callback'.format(i)
        print(page_url)
        data_1 = get_data(page_url)
        parse_data(data_1)
